package net;

import interfaces.Activator;

import java.util.LinkedList;


public class Neuron {
	protected Number output;
	protected LinkedList<Synapse> inputList;
	protected Activator activator;
	protected double errorInformationTerm,correctionTerm,deltaInput ;
	protected Number inputValue;
	protected Bias bias;

	public Neuron(Activator activator) {
		inputList = new LinkedList<Synapse>();
		setActivator(activator);
		errorInformationTerm = 0;
		correctionTerm = 0;
		deltaInput = 0;
		bias = null;
	}
	
	public Neuron(Activator activator,Bias bias) {
		this(activator);
		this.bias = bias;
	}

	
	public void setActivator(Activator activator){
		this.activator = activator;
	}

	public Number getOutput() {
		return output;
	}
	
	public Number getDerived(){
		return activator.functionDerived(output);
	}

	public void addSynapse(Neuron input){
		Synapse synapse = new Synapse(input, this, Math.random());
		inputList.add(synapse);
	}
	
	public Number calculateOutput(){
		Number sum =0;
		if(inputList == null)
			return output;
		else			
			for(Synapse sinapse: inputList)
				sum = sum.doubleValue()+ sinapse.getInput().output.doubleValue()*sinapse.getWeight().doubleValue();
		inputValue = sum.doubleValue();		
		if (bias != null){
			inputValue = inputValue.doubleValue() +  bias.getValue();
		}
		return (output = activationFunction(sum.doubleValue()));
	}
	
	public Number activationFunction(Number x){
		return activator.doActivation(x);
	}

	

	public Number getInputValue() {
		return inputValue;
	}

	public LinkedList<Synapse> getInputList() {
		return inputList;
	}


	public void setInputList(LinkedList<Synapse> ligacoesIn) {
		inputList = ligacoesIn;
	}


	public void setOutput(Number output) {
		this.output = output;
	}

	public double getErrorInformationTerm() {
		return errorInformationTerm;
	}

	public void setErrorInformationTerm(double errorInformationTerm) {
		this.errorInformationTerm = errorInformationTerm;
	}

	public double getCorrectionTerm() {
		return correctionTerm;
	}

	public void setCorrectionTerm(double correctionTerm) {
		this.correctionTerm = correctionTerm;
	}

	public double getDeltaInput() {
		return deltaInput;
	}

	public void setDeltaInput(double deltaInput) {
		this.deltaInput = deltaInput;
	}

	public Bias getBias() {
		return bias;
	}

	public void setBias(Bias bias) {
		this.bias = bias;
	}

	public Activator getActivator() {
		return activator;
	}

	public void setInputValue(Number inputValue) {
		this.inputValue = inputValue;
	}
	
	
	
	
	
}
